<?php

namespace app\admin\controller;


class User extends Common {


    //管理员表
    private $table_admin = "";

    /*
    *	构造函数
    */
    function __construct(){
        parent::__construct();
    }

    //用户列表
    public function lists(){
        $list = db('member')->alias('m')->field('m.id,m.username,m.create_time,m.validity_time,m.type,m.company,m.mobile')->order("id desc")->paginate(10)->each(function($item,$key){
            $member_authority_result = db('member_authority')->where(array('id'=>$item['type']))->field('name')->find();

            if(!empty($member_authority_result)){
                $item['name'] = $member_authority_result['name'];
            }else{
                $item['name'] = '';
            }

            $item['create_time'] = date("Y-m-d",$item['create_time']);

            if($item['validity_time'] != 0){
                $item['validity_time'] = date("Y-m-d",$item['validity_time']);
            }else{
                $item['validity_time'] = '无限期';
            }
            return $item;
        });
        $page = $list->render();

        $this->assign('list',$list);
        $this->assign('page',$page);
        return $this->fetch();
    }

    //编辑用户
    public function edit_user()
    {   
        $id = input('id');
        $user_find = db('member')->where(array('id'=>$id))->field('id,username,create_time,validity_time,type,name,company,mobile,passwd')->find();
        $member_authority = db('member_authority')->where(array('status'=>'1'))->field('id,name')->order('id desc')->select();

        $user_find['create_time'] = date("Y-m-d",$user_find['create_time']);

        if($user_find['validity_time'] != 0){
            $user_find['validity_time'] = date("Y-m-d",$user_find['validity_time']);
        }else{
            $user_find['validity_time'] = '无限期';
        }

        $this->assign('member_authority',$member_authority);
        $this->assign('user_find',$user_find);
        return $this->fetch();
    }

    //编辑用户
    public function edit_user_do()
    {
        if(request()->isAjax()) {
            $id = input('id');
            $name = input('name');
            $company = input('company');
            $mobile = input('mobile');
            $passwd = input('passwd');
            $member_authority = input('member_authority');

            if($name){
                $data['name'] = $name;
            }
            if($company){
                $data['company'] = $company;
            }
            if($mobile){
                $data['mobile'] = $mobile;
            }
            if($passwd){
                $data['passwd'] = md5($passwd);
            }

            $member_authority_result = db('member_authority')->where(array('id'=>$member_authority))->find();
            $user_result = db('member')->where(array('id'=>$id))->field('validity_time,type')->find();

            if(($member_authority != $user_result['type']) && $member_authority != 0){
                $data = array();

                //用户权限更新
                user_input($id, $member_authority);

                $data['type'] = $member_authority;
                db('member')->where(array('id'=>$id))->update($data);
            }
            db('member')->where(array('id'=>$id))->update($data);
            $this->success('修改成功',url('user/lists'));
        }
        
    }

    //编辑用户权限
    public function user_rights(){
        $id = input('id');
        $user_find = db('member')->where(array('id'=>$id))->field('num,username')->find();
        $num = unserialize($user_find['num']);

        if(request()->isAjax()){
            //公式公告
            $formula['formula']['is_condition'] = input('formula_condition');
            $formula['formula']['formula_consult']['is_consult'] = input('formula_consult');
            $formula['formula']['formula_consult']['num'] = input('formula_consult_num');
            $formula['formula']['formula_enclosure']['is_enclosure'] = input('formula_enclosure');
            $formula['formula']['formula_enclosure']['num'] = input('formula_enclosure_num');
            $formula['formula']['formula_down']['is_down'] = input('formula_down');
            $formula['formula']['formula_down']['num'] = input('formula_down_num');
            
            //通知公告
            $circular['circular']['is_condition'] = input('circular_condition');
            $circular['circular']['circular_consult']['is_consult'] = input('circular_consult');
            $circular['circular']['circular_consult']['num'] = input('circular_consult_num');
            $circular['circular']['circular_enclosure']['is_enclosure'] = input('circular_enclosure');
            $circular['circular']['circular_enclosure']['num'] = input('circular_enclosure_num');
            $circular['circular']['circular_down']['is_down'] = input('circular_down');
            $circular['circular']['circular_down']['num'] = input('circular_down_num');


            //政策文件
            $policy['policy']['is_condition'] = input('policy_condition');
            $policy['policy']['policy_consult']['is_consult'] = input('policy_consult');
            $policy['policy']['policy_consult']['num'] = input('policy_consult_num');
            $policy['policy']['policy_enclosure']['is_enclosure'] = input('policy_enclosure');
            $policy['policy']['policy_enclosure']['num'] = input('policy_enclosure_num');
            $policy['policy']['policy_down']['is_down'] = input('policy_down');
            $policy['policy']['policy_down']['num'] = input('policy_down_num');


            //项目公告
            $project_query_num = input('project_query_num');
            $enterprise_follow_num = input('enterprise_follow_num');
            $wechat_tweets_num = input('wechat_tweets_num');
            $interpretation_num = input('interpretation_num');
            $tracking_bulletin_num = input('tracking_bulletin_num');
            $enterprise_planning_num = input('enterprise_planning_num');
            $business_doctor = input('business_doctor');
            $information_maintenance = input('information_maintenance');


            $num = edit_infinite_notice_policy($num,'formula','',$formula);
            $num = edit_infinite_notice_policy($num,'formula','consult',$formula);
            $num = edit_infinite_notice_policy($num,'formula','enclosure',$formula);
            $num = edit_infinite_notice_policy($num,'formula','down',$formula);

            $num = edit_infinite_notice_policy($num,'circular','',$circular);
            $num = edit_infinite_notice_policy($num,'circular','consult',$circular);
            $num = edit_infinite_notice_policy($num,'circular','enclosure',$circular);
            $num = edit_infinite_notice_policy($num,'circular','down',$circular);

            $num = edit_infinite_notice_policy($num,'policy','',$policy);
            $num = edit_infinite_notice_policy($num,'policy','consult',$policy);
            $num = edit_infinite_notice_policy($num,'policy','enclosure',$policy);
            $num = edit_infinite_notice_policy($num,'policy','down',$policy);

            $num = user_edit_jurisdiction($num,'project',$project_query_num);
            $num = user_edit_jurisdiction($num,'enterprise_follow_num',$enterprise_follow_num);
            $num = user_edit_jurisdiction($num,'wechat_tweets_num',$wechat_tweets_num);
            $num = user_edit_jurisdiction($num,'policy_interpretation_num',$interpretation_num);
            $num = user_edit_jurisdiction($num,'tracking_bulletin_num',$tracking_bulletin_num);
            $num = user_edit_jurisdiction($num,'enterprise_planning_num',$enterprise_planning_num);

            $num['business_doctor'] = $business_doctor;
            $num['information_maintenance'] = $information_maintenance;

            $result = db('member')->where(array('id'=>$id))->update(array('num'=>serialize($num)));

            if(!$result)$this->error('修改失败');

            $this->success('操作成功',url('user/lists'));
        }

        $this->assign('user_find',$user_find);
        $this->assign('num',$num);
        $this->assign('id',$id);
        return $this->fetch();
    }

    //会员等级列表
    public function user_authority_list()
    {
        $list = db('member_authority')->where(array('status'=>'1'))->order('id','desc')->paginate(10)->each(function($item,$key){
             $item['formula'] = unserialize($item['formula']);
             $item['circular'] = unserialize($item['circular']);
             $item['policy'] = unserialize($item['policy']);
             return $item;
        });

        $page = $list->render();

        $this->assign('list',$list);
        $this->assign('page',$page);
        return $this->fetch();
    }

    //用户等级编辑
    public function edit_authority()
    {
        $id = input('id');

        $member_authority_result = db('member_authority')->where(array('id'=>$id))->field('name,price,term')->find();

        $this->assign('id',$id);
        $this->assign('member_authority_result',$member_authority_result);
        return $this->fetch();
    }

    //编辑等级
    public function edit_authority_do()
    {
        if(request()->isAjax()) {
            $price = input('price');
            $term = input('term');
            $id = input('id');

            $data['price'] = $price*100;
            $data['term'] = $term;

            $result = db('member_authority')->where(array('id'=>$id))->update($data);

            if(!$result)$this->error('修改失败');

            $this->success('修改成功',url('user/user_authority_list'));
        }
    }

    //会员等级删除
    public function authority_del()
    {
        $id = input('id');

        $member_authority_result = db('member_authority')->where(array('id'=>$id))->update(array('status'=>'-1'));

        if(!$member_authority_result)$this->error('删除失败');
        
        $this->success('删除成功',url('user/user_authority_list'));
    }

    //会员添加
    public function add_authority()
    {
        return $this->fetch();
    }

    //会员等级添加
    public function add_authority_do()
    {
        if(request()->isAjax()) {
            $name = input('name');

            //项目公告
            $project_query_num = input('project_query_num');

            //公式公告
            $formula_condition = input('formula_condition');
            $formula_consult = input('formula_consult');
            $formula_consult_num = input('formula_consult_num');
            $formula_enclosure = input('formula_enclosure');
            $formula_enclosure_num = input('formula_enclosure_num');
            $formula_down = input('formula_down');
            $formula_down_num = input('formula_down_num');

            //通知公告
            $circular_condition = input('circular_condition');
            $circular_consult = input('circular_consult');
            $circular_consult_num = input('circular_consult_num');
            $circular_enclosure = input('circular_enclosure');
            $circular_enclosure_num = input('circular_enclosure_num');
            $circular_down = input('circular_down');
            $circular_down_num = input('circular_down_num');

            //政策文件
            $policy_condition = input('policy_condition');
            $policy_consult = input('policy_consult');
            $policy_consult_num = input('policy_consult_num');
            $policy_enclosure = input('policy_enclosure');
            $policy_enclosure_num = input('policy_enclosure_num');
            $policy_down = input('policy_down');
            $policy_down_num = input('policy_down_num');

            $enterprise_follow_num = input('enterprise_follow_num');
            $wechat_tweets_num = input('wechat_tweets_num');
            $interpretation_num = input('interpretation_num');
            $tracking_bulletin_num = input('tracking_bulletin_num');
            $enterprise_planning_num = input('enterprise_planning_num');
            $business_doctor = input('business_doctor');
            $information_maintenance = input('information_maintenance');
            $price_num = input('price_num');
            $term_num = input('term_num');
            
            $member_authority_result = db('member_authority')->where(array('status'=>'1','name'=>$name))->find();
            if(!empty($member_authority_result)){
                $this->error('该会员等级名字有重复');
            }

            $data['name'] = $name;

            //公式公告
            $formula = array();
            $formula['formula_condition'] = $formula_condition;
            $formula['formula_consult'] = $formula_consult;
            $formula['formula_consult_num'] = $formula_consult_num;
            $formula['formula_enclosure'] = $formula_enclosure;
            $formula['formula_enclosure_num'] = $formula_enclosure_num;
            $formula['formula_down'] = $formula_down;
            $formula['formula_down_num'] = $formula_down_num;
            $data['formula'] = serialize($formula);

            //通知公告
            $circular = array();
            $circular['circular_condition'] = $circular_condition;
            $circular['circular_consult'] = $circular_consult;
            $circular['circular_consult_num'] = $circular_consult_num;
            $circular['circular_enclosure'] = $circular_enclosure;
            $circular['circular_enclosure_num'] = $circular_enclosure_num;
            $circular['circular_down'] = $circular_down;
            $circular['circular_down_num'] = $circular_down_num;
            $data['circular'] = serialize($circular);

            //政府文件
            $policy = array();
            $policy['policy_condition'] = $policy_condition;
            $policy['policy_consult'] = $policy_consult;
            $policy['policy_consult_num'] = $policy_consult_num;
            $policy['policy_enclosure'] = $policy_enclosure;
            $policy['policy_enclosure_num'] = $policy_enclosure_num;
            $policy['policy_down'] = $policy_down;
            $policy['policy_down_num'] = $policy_down_num;
            $data['policy'] = serialize($policy);

            //项目查询
            $data['project'] = $project_query_num;
            $data['enterprise_follow_num'] = $enterprise_follow_num;
            $data['wechat_tweets_num'] = $wechat_tweets_num;
            $data['policy_interpretation_num'] = $interpretation_num;
            $data['tracking_bulletin_num'] = $tracking_bulletin_num;
            $data['enterprise_planning_num'] = $enterprise_planning_num;
            $data['business_doctor'] = $business_doctor;
            $data['information_maintenance'] = $information_maintenance;
            $data['price'] = $price_num*100;
            $data['term'] = $term_num;

            $result = db('member_authority')->insert($data);

            if(!$result)$this->error('添加失败');

            $this->success("操作成功!",url('user/user_authority_list'));
        }
    }

    public function ajax_read_right(){
        $id = input('id');
        $user_find = db('member')->where('id', $id)->field('num,username')->find();
        $num = unserialize($user_find['num']);
    //    halt($num);

        $this->assign('num', $num);
        $this->assign('user_find',$user_find);
        return $this->fetch();
    }
}